//
//  HomeRadioTableViewCell.swift
//  FM
//
//  Created by x on 2017/8/30.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol HomeRadioTableViewCellDelegate:NSObjectProtocol {
    func radioDetailsButtonWillClick(radio:Radio)
}

class HomeRadioTableViewCell: UITableViewCell {

    weak var delegate:HomeRadioTableViewCellDelegate?
    
    /// 数据源显示
    var radios:[Radio]? {
        didSet{
            guard radios!.count == 0 else {
                for i in 0..<contentView.subviews.count {
                    let btu = contentView.viewWithTag(i+1) as! RadioButton
                    let radio = radios![i]
                    btu.setTitle(radio.title, for: UIControlState.normal)
                    SDWebImageManager.shared().loadImage(with: radio.coverImageURL, options: SDWebImageOptions.init(rawValue: 0), progress: nil, completed: { (image, _, error, _, finish, url) in
                        if error == nil{
                            btu.setImage(image, for: UIControlState.normal);
                        }

                    })
//                    SDWebImageManager.shared().downloadImage(with: radio.coverImageURL, options: SDWebImageOptions.init(rawValue: 0), progress: nil, completed: { (image, error, _, _, _) in
//                        if error == nil{
//                            btu.setImage(image, for: UIControlState.normal);
//                        }
//                    })
                }
                return
            }
            
        }
    }
    
    /// 按钮点击事件
    ///
    /// - Parameter btu: <#btu description#>
    func btuClick(btu:UIButton) -> Void {
        delegate?.radioDetailsButtonWillClick(radio: radios![btu.tag-1])
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //初始化
        setupUI()
    }
    
    func setupUI() -> Void {
        
        //1.添加子控件
        contentView.addSubview(btu)
        contentView.addSubview(btu1)
        contentView.addSubview(btu2)
        contentView.addSubview(btu3)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = (bounds.width-40)/4
        let h = bounds.height
        
        //设置按钮的frame
        for i in 0..<contentView.subviews.count {
            
            let btu = contentView.viewWithTag(i+1) as! RadioButton
            btu.frame = CGRect.init(x: CGFloat(i)*(w+10)+(i == 0 ? 5 : 0), y: 0, width: w, height: h)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -- 懒加载
    private lazy var btu:RadioButton = RadioButton.creatRadioButton(tag: 1, target: self, action: #selector(btuClick(btu:)))
    
    private lazy var btu1:RadioButton = RadioButton.creatRadioButton(tag: 2, target: self, action: #selector(btuClick(btu:)))
    
    private lazy var btu2:RadioButton = RadioButton.creatRadioButton(tag: 3, target: self, action: #selector(btuClick(btu:)))
    
    private lazy var btu3:RadioButton = RadioButton.creatRadioButton(tag: 4, target: self, action: #selector(btuClick(btu:)))
}



